package com.lan.mylibrary.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;

import com.lan.mylibrary.R;


/**
 * 项目名：zhihu
 * 包名： com.lanxiong.zhihu.com.lan.mylibrary.util
 * 创建人：Lan
 * 创建时间：2018/4/30 19:48
 * 描述：  自定义TextView  默认颜色 点击颜色  四个corner 同时设定或单独设定
 */


public class MyTextView extends android.support.v7.widget.AppCompatTextView {


    private int colorPressed;
    private int colorNormal;
    private float corners;
    private float leftTopCorner;
    private float leftBottomCorner;
    private float rightTopCorner;
    private float rightBottomCorner;


    public MyTextView(Context context) {
        this(context,null);
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initMyTextView(context, attrs);
    }


    private void initMyTextView(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);
        colorPressed = typedArray.getColor(R.styleable.MyTextView_colorPressed, 0x00000000);
        colorNormal = typedArray.getColor(R.styleable.MyTextView_colorNormal, 0x00000000);
        corners = typedArray.getDimensionPixelSize(R.styleable.MyTextView_corners, 0);
        leftTopCorner = typedArray.getDimensionPixelSize(R.styleable.MyTextView_leftTopCorner, 0);
        leftBottomCorner = typedArray.getDimensionPixelSize(R.styleable.MyTextView_leftBottomCorner, 0);
        rightTopCorner = typedArray.getDimensionPixelSize(R.styleable.MyTextView_rightTopCorner, 0);
        rightBottomCorner = typedArray.getDimensionPixelSize(R.styleable.MyTextView_rightBottomCorner, 0);
        typedArray.recycle();

        setPressedState(colorPressed, colorNormal, corners,leftTopCorner,leftBottomCorner,rightTopCorner,rightBottomCorner);
    }


    private void setPressedState(int colorPressed, int colorNormal, float corners, float leftTopCorner,
                                 float leftBottomCorner, float rightTopCorner,float rightBottomCorner) {

        GradientDrawable pressedDrawable = new GradientDrawable();
        GradientDrawable normalDrawable = new GradientDrawable();

        pressedDrawable.setColor(colorPressed);
        normalDrawable.setColor(colorNormal);

        if (corners>0) {
            pressedDrawable.setCornerRadius(corners);
            normalDrawable.setCornerRadius(corners);
        }

        //1、2两个参数表示左上角，3、4表示右上角，5、6表示右下角，7、8表示左下角
        if (leftTopCorner!=0||leftBottomCorner!=0||rightTopCorner!=0||rightBottomCorner!=0) {
            pressedDrawable.setCornerRadii(new float[]{leftTopCorner,leftTopCorner,rightTopCorner, rightTopCorner,
                    rightBottomCorner, rightBottomCorner, rightBottomCorner, rightBottomCorner});

            normalDrawable.setCornerRadii(new float[]{leftTopCorner,leftTopCorner,rightTopCorner, rightTopCorner,
                    rightBottomCorner, rightBottomCorner, rightBottomCorner, rightBottomCorner});
        }

        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressedDrawable);
        stateListDrawable.addState(new int[]{}, normalDrawable);

        setBackground(stateListDrawable);
    }
}




//<declare-styleable name="MyTextView">
//<attr name="colorPressed" format="reference"/>
//<attr name="colorNormal" format="reference"/>
//<attr name="corners" format="dimension"/>
//<attr name="leftTopCorner" format="dimension"/>
//<attr name="leftBottomCorner" format="dimension"/>
//<attr name="rightTopCorner" format="dimension"/>
//<attr name="rightBottomCorner" format="dimension"/>
//</declare-styleable>
